//
//  SLStatusRetweetView.swift
//  0319iOSweiboDemo
//
//  Created by Tang on 16/3/27.
//  Copyright © 2016年 大天朝. All rights reserved.
//

import UIKit
import SnapKit
//被转发微博的view
class SLStatusRetweetView: UIView {
    
    //MARK: - 属性
    var retweetViewBottomCons: Constraint?
    
    //被转发的微博模型
    var retweetStatus: SLStatus? {
        didSet {
                //当别人转发的模型赋值后,将模型的内容赋值给对应的view
                //@发微博人的名字:微博内容
                guard let screen_name = retweetStatus?.user?.screen_name
                    else {
                    SLPrint(msg: "retweetedStatus?.user?.screen_name 没有值")
                    return
                }

                guard let text = retweetStatus?.text else {
                    SLPrint(msg: "retweetedStatus?.text 没有值")
                    return
                }
                retweetContenLabel.text = "@\(screen_name): \(text)"
                
                self.retweetViewBottomCons?.uninstall()// 删除约束
                //判断被转发的微博里面是否有配图
                if let picutureURLs = retweetStatus?.storePictureURLs {
                    //有图片要显示
                    retweetPictureView.hidden = false
                    
                    //将被转发微博的图片URL赋值给retweetPictureView
                    retweetPictureView.pictureURLs = picutureURLs
                    
                    //重新设置SLStatusRetweetView的底部约束
                    self.snp_makeConstraints(closure: { (make) -> Void in
                        self.retweetViewBottomCons = make.bottom.equalTo(retweetPictureView).offset(SLStatusCellMargin).constraint
                    })
                } else {
                    //没有图片要显示
                    retweetPictureView.hidden = true
                    self.snp_makeConstraints(closure: { (make) -> Void in
                        self.retweetViewBottomCons = make.bottom.equalTo(retweetContenLabel).offset(SLStatusCellMargin).constraint
                    })
                }
            }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    
    private func prepareUI() {
        //添加子控件
        addSubview(bkgButton)
        addSubview(retweetContenLabel)
        addSubview(retweetPictureView)
        
        //添加约束
        bkgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        //被转发的微博
        retweetContenLabel.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(self).offset(SLStatusPictureManager)
            make.trailing.equalTo(self).offset(-SLStatusPictureManager)
        }
        
        //被转发的微博配图
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetContenLabel)
            make.top.equalTo(retweetContenLabel.snp_bottom).offset(SLStatusPictureManager)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        //被SLStatusRetweetView的底部和retweetContenLabel重合.SLStatusRetweetView的高度根据子控件的内容确定
        self.snp_makeConstraints { (make) -> Void in
           self.retweetViewBottomCons = make.bottom.equalTo(retweetPictureView).offset(SLStatusCellMargin).constraint
        }
        
    }

   //MARK: - 懒加载
    //背景按钮
    private lazy var bkgButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
    
    //被转发微博
    private lazy var retweetContenLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(SLStatusCellContentSize)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        //换行(折行)的宽度
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * SLStatusCellMargin
        return label
    }()

    //微博配图
    private lazy var retweetPictureView: SLStatusPictureView  = SLStatusPictureView()

}
